<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2017/6/23
 * Time: 11:14
 */
class Goods extends Admin_Controller {
    public function __construct()
    {
        parent::__construct();
        //$this->load->library('form_validation');
        $this->load->library('Pagination');
        $this->load->model('Goods_model');
        $this->load->model('Brand_model');
        $this->load->model('category_model');
    }
    //增
    public function goods_add(){
        if ($_POST){
            //var_dump($_POST);die;
            #获取提交的数据
            $data['goods_name'] = $this->input->post('goods_name');
            $data['goods_sn'] = $this->input->post('goods_sn');
            $data['cat_id'] = $this->input->post('cat_id');
            $data['brand_id'] = $this->input->post('brand_id');
            $data['suppliers_id'] = $this->input->post('suppliers_id');
            $data['shop_price'] = $this->input->post('shop_price');
            $data['user_price'] = $this->input->post('user_price');
            $data['market_price'] = $this->input->post('market_price');
            $data['is_promote'] = $this->input->post('is_promote');
            $data['promote_price'] = $this->input->post('promote_price');
            $data['promote_start_time'] = strtotime($this->input->post('promote_start_time'));
            $data['promote_end_time'] = strtotime($this->input->post('promote_end_time'));
            $data['goods_desc'] = $this->input->post('goods_desc');
            $data['goods_weight'] = $this->input->post('goods_weight');
            $data['goods_number'] = $this->input->post('goods_number');
            $data['warn_number'] = $this->input->post('warn_number');
            $data['is_best'] = $this->input->post('is_best');
            $data['is_new'] = $this->input->post('is_new');
            $data['is_hot'] = $this->input->post('is_hot');
            $data['is_onsale'] = $this->input->post('is_onsale');
            $data['is_alone_sale'] = $this->input->post('is_alone_sale');
            $data['is_shipping'] = $this->input->post('is_shipping');
            $data['keywords'] = $this->input->post('keywords');
            $data['goods_brief'] = $this->input->post('goods_brief');
            $data['type_id'] = $this->input->post('type_id');

            #完成上传图片
            $config['upload_path'] = './uploads/goods/' . date('Ymd') . '/';
            if (!file_exists($config['upload_path'])) {
                mkdir($config['upload_path']);
            }
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = 200;
            $this->load->library('upload',$config);
            if ($this->upload->do_upload('goods_img')) {
                # 上传成功，缩略处理
                $res = $this->upload->data(); //获取上传图片信息
                $data['goods_img'] = date('Ymd').'/'.$res['file_name'];
                $config_img['image_library'] = 'gd2';
                $config_img['source_image'] = './uploads/goods/'.date('Ymd').'/' . $res['file_name'];
                $config_img['create_thumb'] = true;
                $config_img['maintain_ratio'] = true;
                $config_img['width'] = 80;
                $config_img['height'] = 80;
                #载入并初始化图像处理类
                $this->load->library('image_lib',$config_img);
                if ($this->image_lib->resize()) {
                    # 缩略ok,得到缩略图的名称
                    $data['goods_thumb'] = date('Ymd').'/'.$res['raw_name'] . $this->image_lib->thumb_marker. $res['file_ext'];
                    var_dump($data);die;

                    if ($goods_id = $this->goods_model->add_goods($data)) {
                        # 添加商品成功,获取属性并插入到商品属性关联表中
                        $attr_ids = $this->input->post('attr_id_list');
                        $attr_values = $this->input->post('attr_value_list');
                        foreach ($attr_values as $k => $v) {
                            if (!empty($v)) {
                                $data2['goods_id'] = $goods_id;
                                $data2['attr_id'] = $attr_ids[$k];
                                $data2['attr_value'] = $v;
                                #完成插入
                                $this->db->insert('goods_attr',$data2);
                            }
                        }
                        $data['message'] = '添加商品成功';
                        $data['url'] = site_url('admin/goods/index');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);


                    } else {
                        # 失败
                        $data['message'] = '添加商品失败';
                        $data['url'] = site_url('admin/goods/add');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }


                } else {
                    # 缩略失败
                    $data['message'] = $this->image_lib->display_errors();
                    $data['url'] = site_url('admin/goods/add');
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }


            } else {
                # 上传失败
                $data['message'] = $this->upload->display_errors();
                $data['url'] = site_url('admin/goods/add');
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }
        }else{
            $data['brands'] = $this->Brand_model->list_brand();
            $data['cates'] = $this->category_model->list_cate();
            $this->load->view('Goods/goods_add.html',$data);
        }
    }
    //删
    public function goods_delete(){

    }
    //改
    public function goods_edit(){
        if ($_POST){

        }else{
            $this->load->view('Goods/goods_edit.html');
        }
    }
    //查
    public function goods_list(){
        $data['goodses'] = $this->Goods_model->list_goods();
        $this->load->view('Goods/goods_list.html',$data);
    }
}